Skip to content

Fix flake8 error lambda functions (E731) with private function for pattern matching - #80

Closed
bobleesj wants to merge 5 commits into
diffpy:cookiefrom
bobleesj:cookie-lambda-func
Closed

bobleesj wants to merge 5 commits into
diffpy:cookiefrom
bobleesj:cookie-lambda-func

Conversation

@bobleesj

Copy link
Copy Markdown

camelCase is used here.

Addressing the following https://github.com/diffpy/diffpy.srfit/pull/78/files#r1724308313

Screenshot 2024-08-21 at 6 24 15 PM

Tests pass

WARNING:diffpy.srfit.tests:No module named 'sas', SaS tests skipped.
WARNING:diffpy.srfit.tests:Cannot import pyobjcryst, pyobjcryst tests skipped.
WARNING:diffpy.srfit.tests:Cannot import diffpy.srreal, PDF tests skipped.
......ssss................................sssssssssss....ssssss...........................sss..s..............
----------------------------------------------------------------------
Ran 110 tests in 0.254s

@bobleesj bobleesj left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please review @sbillinge

"""
regexp = re.compile(pattern)
pmatch = lambda s: (len(s.split(None, 1)) < 2 or regexp.search(s.split(None, 1)[0]))
self._regexp = re.compile(pattern)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regexp is used twice in the function, storing as a private var.

rlines.sort(key=numstr)
return rlines

def _matchPattern(self, s):

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

following the camelCase convention.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have moved away from camelCase to be more pep8 compliant. I think that at least where we touch the code we may want to remove CC and certainly not add it. It means we will have mixed standards in the code, but on balance I prefer moving code forward when we can.

The function name seems to be a bit vague. Of course it does match a pattern, so matchPattern is strictly correct, but the idea is to make it code self commenting. We do this by trying to pick names that capture what the function does. Here it is being used explicitly for something, not as an abstract function for matching patterns, so if we can figure out what it is being used for and capture that in the name, it makes the code better

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted. Will implement the pep8 standards and make function names specific so that it documents by itself

@bobleesj
bobleesj marked this pull request as ready for review August 21, 2024 22:26
@bobleesj bobleesj changed the title Fix flake8 error lambda functions (E731) with private function Fix flake8 error lambda functions (E731) with private function for pattern matching Aug 21, 2024

@sbillinge sbillinge left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see comment

rlines.sort(key=numstr)
return rlines

def _matchPattern(self, s):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have moved away from camelCase to be more pep8 compliant. I think that at least where we touch the code we may want to remove CC and certainly not add it. It means we will have mixed standards in the code, but on balance I prefer moving code forward when we can.

The function name seems to be a bit vague. Of course it does match a pattern, so matchPattern is strictly correct, but the idea is to make it code self commenting. We do this by trying to pick names that capture what the function does. Here it is being used explicitly for something, not as an abstract function for matching patterns, so if we can figure out what it is being used for and capture that in the name, it makes the code better

@bobleesj

Copy link
Copy Markdown
Author

What do you think about renaming the function to _is_single_or_matches_pattern instead of _matchPattern? This function essentially returns true or false, depending on whether it's a single word or if the first word matches the pattern. @sbillinge

@bobleesj

Copy link
Copy Markdown
Author

@sbillinge Just wanted to bring this up for your review.

@sbillinge

Copy link
Copy Markdown
Contributor

I guess I still am not sure what it does and why

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants